package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.shenzhen.horselive.MainActivity;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.api.response.MyInfoResponse;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.IsBindBean;
import com.shenzhen.horselive.databinding.ActivityBIndPhoneBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

public class BindPhoneActivity extends AppBarActivity {

    ActivityBIndPhoneBinding   binding;
    MyInfoResponse  myInfoResponse ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBIndPhoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        myInfoResponse =  User.get().getMyInfo();
        //状态栏设置
        setStyleRed();
        setTitle("绑定手机号");
        //初始化
        initView();
    }

    CountDownTimer  countDownTimer;
    private void initView() {
        //发送验证码
        binding.tvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String  phone =  binding.etPhone.getText().toString();
                if (TextUtils.isEmpty(phone))
                {
                    return;
                }

                if (countDownTimer==null)
                {
                    //30秒倒计时
                    countDownTimer = new CountDownTimer(60000,1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            binding.tvGetCode.setClickable(false);
                            binding.tvGetCode.setText((int) (millisUntilFinished/1000)+"s");
                        }

                        @Override
                        public void onFinish() {
                             binding.tvGetCode.setClickable(true);
                             binding.tvGetCode.setText("发送验证码");
                        }
                    };
                }
                countDownTimer.start();
                //发送
                sendCode(phone);
            }
        });

        //提交注册信息
        binding.btBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    //发送验证码
    private void sendCode(String phone) {
        ApiFactory.getApiService().sendCode(phone,"1")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            Toast.makeText(BindPhoneActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });
    }

    //提交注册信息
    private void submit() {
        String  phone =  binding.etPhone.getText().toString();
        if (TextUtils.isEmpty(phone))
        {
            showToast("手机号码不能为空");
            return;
        }
        String  code =  binding.etCode.getText().toString();
        if (TextUtils.isEmpty(code))
        {
            showToast("验证码不能为空");
            return;
        }
        String  tuijian = binding.etInviteCode.getText().toString();
        if (TextUtils.isEmpty(tuijian))
        {
            showToast("推荐人手机号码不能为空");
            return;
        }
        ApiFactory.getApiService().register(myInfoResponse.getAccessToken(),phone,code,myInfoResponse.getOpenid(),tuijian.trim())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<IsBindBean>(this) {
                    @Override
                    protected void onNextInActive(IsBindBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            //注册成功
                            showToast("绑定成功");
                            User.get().storeMyInfo(commResponseBean.getData());
                            User.get().setLogin(true);
                            startActivity(new Intent(BindPhoneActivity.this, MainActivity.class));
                            finish();
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //回收一下
        if (countDownTimer!=null)
        {
            countDownTimer.cancel();
            countDownTimer=null;
        }
    }
}